package com.industrial.im.controller;

import com.alibaba.fastjson2.JSONObject;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.config.RongCloudConfig;
import com.industrial.im.domain.ImFrequentContacts;
import com.industrial.im.mapper.ImFrequentContactsMapper;
import com.industrial.im.service.ImUserService;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *     Im用户操作
 * </p>
 * @author SangGuangCan
 * @since 2024-03-05
 */

@RestController
@RequestMapping("/im/user")
public class ImUserController {

    @Autowired
    private ImUserService imUserService;

    @Autowired
    private ImFrequentContactsMapper imFrequentContactsMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 注册用户到融云换取Token
     * @return
     */
    @PostMapping("/registerUserInfoToRongCloud")
    public AjaxResult registerUserInfoToRongCloud(@RequestBody Map<String,String> params){
        if(StringUtils.isBlank(params.get("userId"))){
            return AjaxResult.warn("请传入用户ID");
        }
        JSONObject token = imUserService.registerUserInfoToRongCloud(params);
        return AjaxResult.success(token);
    }

    /**
     * 取消常用联系人
     * @return
     */
    @GetMapping("/cacalFcUser/{fcId}")
    public AjaxResult cacalFcUser(@PathVariable("fcId") String fcId){
        imUserService.cacalFcUser(fcId);
        return AjaxResult.success();
    }


    /**
     * 根据用户Id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/queryUserInfo/{fcId}")
    public AjaxResult queryUserInfo(@PathVariable("fcId") String fcId){
        R<SysUser> userInfoByUserId = remoteUserService.getUserInfoByUserId(fcId);
        if(userInfoByUserId.getCode() == R.SUCCESS){
            SysUser userInfo = userInfoByUserId.getData();
            String currentLoginUserId = SecurityUtils.getUserId();
            ImFrequentContacts queryParams = new ImFrequentContacts();
            queryParams.setUserId(currentLoginUserId);
            queryParams.setFcId(fcId);
            List<ImFrequentContacts> result =  imFrequentContactsMapper.selectFcUserList(queryParams);
            if(result != null && !result.isEmpty()){
                userInfo.setFcFalg("1");
            }else{
                userInfo.setFcFalg("0");
            }
            return AjaxResult.success(userInfo);
        }
        return AjaxResult.warn("获取用户信息失败");
    }

    /**
     * 设置常用联系人
     * @params fcId 常用联系人Id
     * @return
     */
    @GetMapping("/setFrequentContacts/{fcId}")
    public AjaxResult setFrequentContacts(@PathVariable("fcId") String fcId){
        imUserService.setFrequentContacts(fcId);
        return AjaxResult.success("设置成功");
    }

    /**
     * 加载常用联系人
     * @return
     */
    @GetMapping("/loadFcUserList")
    public AjaxResult loadFcUserList(){
        List<ImFrequentContacts> imFrequentContacts = imUserService.loadFcUserList();
        return AjaxResult.success(imFrequentContacts);
    }
}
